<%@ page pageEncoding="UTF-8"%>
<%@page import="itsm.isperp.framework.core.context.ContextHolder"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="isperp" uri="/WEB-INF/tlds/isperp.tld"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>

<style>
#individuationPropertyTable h3 {
	font-size: 14px;
	display: inline;
	font-weight: bold;
	margin: 0 9px;
	color: #555;
	height: 28px;
	line-height: 28px;
}
</style>

<div id="individuationPropertyDetail" class="box  no-border">
	<div id="curUserName" style="display:none"><%=ContextHolder.getLoginUser().getFullname()%></div>
	<!-- <div class="header">
		<h3>&nbsp;&nbsp;扩展信息（不同分类有不同的属性）</h3>
	</div> -->
	<div id="individuationPropertyTableLoading"></div>
	<!-- <table id="individuationPropertyTable" class="form-table"
		style="table-layout: auto">
	</table> -->
</div>

<script type="text/javascript">
	//var initPryMap = new Map();//存储页面初始化的资产属性包括基本属性与扩展属性

	$(document).ready(function() {
		setCategoryProperty();

	});

	function setCategoryProperty(ids, names, treeNode) {
		var icdCategoryIdVal = ids || $("#categoryId").val();
		var carbonCategoryId = '${carbonCategoryId}';

		$("#individuationPropertyTableLoading").empty();
		if (icdCategoryIdVal == undefined || icdCategoryIdVal == "" || carbonCategoryId=="") {
			return false;
		}

		$("#individuationPropertyTableLoading").html("载入自定义属性中...");

		$.getJSON("individuation/categoryPropertySetting/getOne?id=" + carbonCategoryId, function(data) {
			var chtml = data.chtml+"<input style=\"display:none\" name=\"tid_individuationProperty\" id=\"tid_individuationProperty\">";
			$("#individuationPropertyTableLoading").empty();
			//直接根据分类获取append的HTML
			$("#individuationPropertyTableLoading").append(chtml);
			//处理列表
			createList();
			setCiPropertyValue();
			isperp.initElement("#individuationPropertyTable");
			//表单字段写权限控制
			var processKey = "individuationProcess";
			var processId = '${model.processId}';
			var id = $("#id").val() || "${model.id}";
			
			var status = '${model.status}';
			if (status == "")
				status = "新建";
			$.ajax({
				type : 'GET',
				url : 'bpm/nodeCategoryRelation/getCannotWriteColumnsAndDefaultValue',
				data : {
					status : status,
					categoryId : icdCategoryIdVal,
					id : id
				},
				success : function(data) {
					var rol = '${readOnly}';
					if (rol == "true") {
						$("[id^='c_']").attr("disabled", "disabled");
						$("[id^='c_']").attr("placeholder", "");
						//针对列表
						$("[rd^='tdaction_list_']").remove();
						$("#individuationPropertyTable select").trigger("chosen:updated");
					}
					if (data.prvColumnsSetting) {

						if (rol == "true") {
							$("[id^='c_']").attr("disabled", "disabled");
							$("[id^='c_']").attr("placeholder", "");
							//针对列表
							$("[rd^='tdaction_list_']").remove();
							$("#individuationPropertyTable select").trigger("chosen:updated");
						} else if (data.prvColumnsSetting.canNotWrite) {
							var t = data.prvColumnsSetting.canNotWrite;
							var canNotWriteColumns = t.split(",");
							for ( var i = 0; i < canNotWriteColumns.length; i++) {
								var cId = "c_" + canNotWriteColumns[i];
								$("[id^='" + cId + "']").attr("disabled", "disabled");
								$("[id^='" + cId + "']").attr("placeholder", "");
								//针对列表
								$("[rd='tdaction_list_" + canNotWriteColumns[i] + "']").remove();
								$("#individuationPropertyTable select").trigger("chosen:updated");
							}
						}

						if (data.prvColumnsSetting.requiredWrite) {
							var t = data.prvColumnsSetting.requiredWrite;
							var requiredWriteColumns = t.split(",");
							for ( var i = 0; i < requiredWriteColumns.length; i++) {
								var cId = "c_" + requiredWriteColumns[i];
								$("[id^='" + cId + "']").addClass("required");
								$("#individuationPropertyTable select").trigger("chosen:updated");
							}
						}

					}
					if (data.defaultColumnValueList.length > 0) {
						var tmpArr = data.defaultColumnValueList;
						for ( var k = 0; k < tmpArr.length; k++) {
							var did = tmpArr[k].id;
							if (data.prvColumnsSetting != null && data.prvColumnsSetting.canNotWrite.indexOf(did) < 0) {
								var tmpDefaultValueStr = tmpArr[k].default_value;
								tmpDefaultValueStr = formatDefaultStr(tmpDefaultValueStr);
								$("#c_" + did).val(tmpDefaultValueStr);
							} else if (data.prvColumnsSetting == null) {
								var tmpDefaultValueStr = tmpArr[k].default_value;
								tmpDefaultValueStr = formatDefaultStr(tmpDefaultValueStr);
								$("#c_" + did).val(tmpDefaultValueStr);
							}
						}

					}

				}
			});
			//存储资产所有属性，当保存时如果存在改变的属性，则提交到后台记录
			//edit at 20150427
			//getPagePropetyMap(initPryMap);

		});

	}

	function createList() {
		$("[dataType='list']").each(function() {
			var objId = $(this).data("id");
			var targetId='div_list_'+objId;
			var objOption=$(this).data("option");
			if (objId) {
				var relationId = $('#id').val();
				var option={targetId:targetId,readOnly:false,relationId:relationId,tableName:'',columns:[]};
				var tOption=eval('(' + objOption + ')');
				option.tableName=tOption.tableName;
				option.columns=tOption.columns;
				isperp.createEasyTable(option);
			}
		});
	}

	function formatDefaultStr(v) {
		if (v.indexOf("$") > -1) {
			var result = v;
			if (v.indexOf("curUserName") > -1) {
				result = $("#curUserName").text();
			} else if (v.indexOf("curDate") > -1) {
				result = getCurentTime();
			}
			return result;
		} else {
			return v;
		}
	}

	function saveProperty() {
		var propertyList = getPropertyControlValues();
		$.post("individuation/property/saveProperty", {
			"relationId" : $("#id").val() || "${model.id}",
			"propertyList" : propertyList
		});
	}

	function getPagePropetyMap(map) {

		$("[id^='basicInfoTable'] input").each(function() {
			var obj_input = $(this);
			var obj_id = $(this).attr("name");
			var obj_value = $(this).val();
			map.put(obj_id, obj_value);

		});
		$("[id^='basicInfoTable'] select").each(function() {
			var obj_input = $(this);
			var obj_id = $(this).attr("name");
			var obj_value = $(this).val();

			map.put(obj_id, obj_value);

		});
		$("[id^='basicInfoTable'] textarea").each(function() {
			var obj_input = $(this);
			var obj_id = $(this).attr("name");
			var obj_value = $(this).val();
			map.put(obj_id, obj_value);

		});

		$("#ciPropertyTable input").each(function() {
			var obj_input = $(this);
			var obj_id = $(this).attr("id");
			var obj_value = $(this).val();
			map.put(obj_id, obj_value);

		});
		$("#ciPropertyTable select").each(function() {
			var obj_input = $(this);
			var obj_id = $(this).attr("id");
			var obj_value = $(this).val();

			map.put(obj_id, obj_value);

		});

		return map;

	}

	//设置自定义ci的属性
	function setCiPropertyValue() {
		var id = $("#id").val() || "${model.id}";
		//控制保密字段
		var creator='${model.creator}';
		var categoryId = $("#categoryId").val();
			var status = '${model.status}';
			if (status == "")
				status = "新建";
		$.ajax({
			url : "individuation/property/listProperty?id=" + id+"&categoryId="+categoryId+"&status="+status+"&creator="+creator,
			async : false,
			dataType : "json",
			success : function(data) {
				$(data).each(function(index, item) {
					if (item.data_type == 'textarea' || item.data_type == 'input' || item.data_type == 'string' || item.data_type == 'date' || item.data_type == 'datetime' || item.data_type == 'select') {
						$("#c_" + item.category_property_id).val(item.value);
					} else if (item.data_type == 'checkboxs') {
						var ta = item.value.split(",");
						$("#c_" + item.category_property_id).val(item.value);
						for ( var i = 0; i < ta.length; i++) {
							var t = $("[name='" + item.category_property_id + "'][value='" + ta[i] + "']").attr("checked", true);
						}

					} else if (item.data_type == 'radios') {
						$("[name='c_" + item.category_property_id + "'][value='" + item.value + "']").attr("checked", true);
					}

				});

			}
		});
	}

	//获取自定义属性的值
	function getPropertyControlValues() {
		var $input = $("#individuationPropertyTable input");
		var $select = $("#individuationPropertyTable select");

		var count = 0;
		var result = "[";
		$($input).each(function(index, item) {
			var $el = $(item);

			if ($el.attr("id") && $el.val()) {

				if (count == 0) {
					result += "{'categoryPropertyId':'" + $el.data("id") + "','value':'" + $el.val() + "'}"
				} else {
					result += ",{'categoryPropertyId':'" + $el.data("id") + "','value':'" + $el.val() + "'}"
				}
				count++;
			}
		});
		$($select).each(function(index, item) {
			var $el = $(item);
			if ($el.attr("id") && $el.val()) {
				if (count == 0) {
					result += "{'categoryPropertyId':'" + $el.data("id") + "','value':'" + $el.val() + "'}"
				} else {
					result += ",{'categoryPropertyId':'" + $el.data("id") + "','value':'" + $el.val() + "'}"
				}
				count++;
			}
		});

		return result + "]";
	}

	//构建input元素
	function createInputElement(item, cssCls) {
		var controlStr = "<input type='" + item.data_type + "' name='c_" + item.id + "' id='c_" + item.id + "' data-id='" + item.id + "' class='" + cssCls;

		if (item.notnull) {
			controlStr += " required";
		}

		controlStr += "' />"
		return controlStr;
	}

	//构建自定义属性输入控件
	function createControl(item) {
		var controlStr = "";
		var cssCls = "";
		switch (item.data_type) {
		case "string":
			controlStr = createInputElement(item, cssCls);
			break;
		case "number":
			cssCls = "number";
			item.data_type = "text";
			controlStr = createInputElement(item, cssCls);
			break;
		case "date":
		case "datetime":
			controlStr = createInputElement(item, cssCls);
			break;
		case "textarea":
			controlStr = "<textarea name='c_" + item.id + "'  id='c_" + item.id + "' data-id='" + item.id + "' class='" + cssCls;

			if (item.notnull) {
				controlStr += " required";
			}

			controlStr += "'></textarea>";
			break;

		case "select":
			var itemDataSource = $.parseJSON(item.items);
			controlStr = "<select name='c_" + item.id + "' id='c_" + item.id + "'   data-id='" + item.id + "' ";
			if (item.notnull) {
				controlStr += " class='required'>";
			} else {
				controlStr += " >";
			}
			controlStr += "<option value=''></option>";
			if (itemDataSource) {
				$(itemDataSource).each(function(i, it) {
					controlStr += "<option value='"+it.value+"'>" + it.name + "</option>";
				});
			}
			controlStr += "</select>";
			break;
		case "custom":
			controlStr += item.items;
			break;
		case "checkboxs":
			var itemDataSource = eval('(' + item.items + ')');
			controlStr += "<span style='display:none'><input id='c_"+item.id+"' name='c_"+item.id+"' ></span>";
			if (itemDataSource) {
				$(itemDataSource).each(function(i, it) {
					controlStr += "<span><input id='c_" + item.id + i + "' onclick=\"c_checkbox_click(this)\" name='" + item.id + "' type='checkbox' value='" + itemDataSource[i].value + "'><label for='c_"+item.id+i+"'>" + itemDataSource[i].name + "</label></span>";
				});
			}
			break;
		case "radios":
			var itemDataSource = eval('(' + item.items + ')');
			if (itemDataSource) {
				$(itemDataSource).each(function(i, it) {
					controlStr += "<span><input id='c_"+item.id+"_"+i+"' name='c_"+item.id+"' type='radio' value='"+itemDataSource[i].value+"'><label for='c_"+item.id+"_"+i+"'>" + itemDataSource[i].name + "</label></span>";
				});
			}
			break;

		}
		return controlStr;

	}
	function c_checkbox_click(obj) {
		var tId = obj.name;
		var checkValue = "";
		if ($("#c_" + tId).val() != "") {
			checkValue = $("#c_" + tId).val() + ","
		}
		if (obj.checked) {
			if (checkValue.indexOf(obj.value) < 0) {
				checkValue += obj.value + ",";
			}
		} else if (!obj.checked) {
			if (checkValue.indexOf(obj.value) > -1) {
				checkValue = checkValue.replace((obj.value + ","), "");
			}
		}
		checkValue = checkValue.substring(0, checkValue.length - 1);
		$("#c_" + tId).val(checkValue);
	}
	function strToJson(str) {
		return JSON.parse(str);
	}
	function getCurentTime() {
		var now = new Date();
		var year = now.getFullYear(); //年
		var month = now.getMonth() + 1; //月
		var day = now.getDate(); //日
		var hh = now.getHours(); //时
		var mm = now.getMinutes(); //分
		var clock = year + "-";
		if (month < 10)
			clock += "0";
		clock += month + "-";
		if (day < 10)
			clock += "0";
		clock += day + " ";
		if (hh < 10)
			clock += "0";
		clock += hh + ":";
		if (mm < 10)
			clock += '0';
		clock += mm;
		return (clock);
	}
</script>